<!DOCTYPE html>
<html>
<head>

	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
	<title>图片</title>
</head>
<style type="text/css">
	*{
		padding: 0px;
	    margin: 0px;
	    box-sizing: border-box;
	}
	#imgViewer{
		/*display: none;*/
	    /*opacity: 0;*/
	    position: fixed;
	    overflow: hidden;
	    top: 0;
	    left: 0;
	    width: 100%;
	    height: 100%;
	    z-index: 1001;
	    background-color: #222;
	    background-color: rgba(0,0,0,.8);
	    -webkit-transition: opacity .5s ease;
	    transition: opacity .5s ease;
		opacity: 1;
	}
	#imgBox{
		position: absolute;
	    left: 0;
	    top: 0;
	    height: 100%;
	    width: 100%;
	    white-space: nowrap;
	    -webkit-transition: left .4s ease,
	    -webkit-transform .4s ease;
	    transition: left .4s ease,-moz-transform .4s ease;
	    transition: left .4s ease,transform .4s ease;
	    transform: translate3d(0%,0px,0px);
		
	}

	.full-image{
		display: inline-block;
	    position: relative;
	    top: 0;left: 0;right: 0;bottom: 0;
	    width: 100%;
	    height: 100%;
	    text-align: center;
	}
	.full-image img{
		display: inline-block;
	    width: 100%;
	    vertical-align: middle;
	    -webkit-box-shadow: 0 0 8px rgba(0,0,0,.6);
	    -moz-box-shadow: 0 0 8px rgba(0,0,0,.6);
	    box-shadow: 0 0 8px rgba(0,0,0,.6);
	    margin: auto;
	    position: absolute;
	    top: 0;left: 0;right: 0;bottom: 0;
	}

	#btn{
		width: 300px;
		height: 100px;
		margin:50px auto;
		line-height:100px;
		z-index: 1002;
		position: absolute;
	    top: 0;left: 0;right: 0;bottom: 0;
	}
	#btn a{
		margin-left: 30px;
		padding: 30px 30px;
		border: 1px solid #666;
		border-radius:10px;
		text-decoration: none;
		font-size: 20px;
		/*z-index: 1003;*/
	}
</style>
<body>




<div id="imgViewer">
	<div id="btn">
		<a href="javascript:enlarge();">放大</a>
		<a href="javascript:reduce();">缩小</a>
		<a href="javascript:goLeft();">向左</a>
		<a href="javascript:goRight();">向右</a>
	</div>
	<div id="imgBox">
		<div class="full-image" id="img-0" style="">
			<img src="./img/tibet-1.jpg">
		</div>

		<div class="full-image" id="img-1">
			<img src="./img/tibet-2.jpg">
		</div>

		<div class="full-image" id="img-2">
			<img src="./img/tibet-3.jpg">
		</div>

		<div class="full-image" id="img-3">
			<img src="./img/tibet-4.jpg">
		</div>

		<div class="full-image" id="img-4">
			<img src="./img/tibet-5.jpg">
		</div>
	</div>
</div>

<script type="text/javascript">

var w =  window.screen.width;

var imgBox = document.getElementById('imgBox');
var length = imgBox.getElementsByTagName('img').length-1;
var img = document.getElementById('img-0');

function positiveInt(number) {
	if (Number(number) >= 0) {
	   return true;
	}
	return false;
}

function setTranslate3d(M) {
	// console.log(M);
	var t = 101.3 * -M + "%";
    imgBox.style.transform = imgBox.style.webkitTransform = "translate3d(" + t + ",0,0)";
}
 
window.onload = function () {
	var startX,index=0,distanceX;
	var isMove = false,isTouch=false;
	var now, delta; //当前时间，两次触发事件时间差
	imgBox.addEventListener('touchstart',function (e) {
		startX = e.touches[0].clientX;
		// console.log(e.touches[0].clientX,e.touches[0].clientY);
		if (e.touches.length >= 2) { //判断是否有两个点在屏幕上
            
        } else {
            delta = Date.now() - now; //计算两次点击时间差
            now = Date.now();
            if (delta > 0 && delta <= 250) { //双击事件
// 　　　　　　　　　console.log('双击事件');
            } else { //滑动事件
            	// console.log('滑动事件');
            	startX = e.touches[0].clientX;
				// console.log(e.touches[0].clientX,e.touches[0].clientY);
            }
            isTouch = true;
        }
		
	});
	imgBox.addEventListener('touchmove',function (e) {
		var moveX = e.touches[0].clientX;
        /*计算位移  有正负方向*/
        // 手指向左方向移动时，移动距离为负 index加1，图片向右移动
        // 手指向右方方向移动，移动距离为正 index减1，图片向左移动
        distanceX = moveX - startX;
        isMove= true;
        console.log(isMove);
		
	});
	imgBox.addEventListener('touchend',function (e) {
		// 单击与滑动得判断，判断有没有位移
		// 没有位移则为单击
		// 有位移则为双击
		if (isMove) {
			if (positiveInt(distanceX)) {
			index<=0?index=0:index--;	
	        }else{
	        	index>=length?index = length:index++;
	        }
	        setTranslate3d(index);
		}
		isTouch=false;
		startX = 0;
        distanceX = 0;
        isMove = false;
		
		
	});
	imgBox.addEventListener('gesturechange ',function (e) {
		console.log('ongestureend ');
		alert('nothing');
		
	});
	function handleGestureEvent() {
		// body...
		alert('驾驶的飞机');
	}
	document.addEventListener("gesturestart", handleGestureEvent, false);
	document.addEventListener("gestureend", handleGestureEvent, false);
	document.addEventListener("gesturechange", handleGestureEvent, false);
}
// alert('nothing');
var Touch = {
	obj:null,
	isMove:false,
	isDrag:false,
	isTouch:false,
	isDouble:false,
	isMultipoint:false,
	lastTime:null,
	bind:function (eventType,callback) {
		if(window.addEventListener){
			this.obj.addEventListener(eventType, callback, false);
		}else{
		    //ie浏览器
			this.obj.attachEvent('on'+eventType, callback);
		}
	},
	// 单击
	touch:function (callback) {
		this.bind('touchstart',function (e) {
			e.preventDefault();

			if (e.touches.length >= 2) { //判断是否有两个点在屏幕上
            	this.isMultipoint = true;
	        } else {
	            var delta = Date.now() - this.lastTime; //计算两次点击时间差
	            this.lastTime = Date.now();
	            if (delta > 0 && delta <= 250) { //双击事件
	　　　　　　　　　this.isDouble = true;
	            } else { //滑动事件
	            	this.isTouch = true;
	            }
	        }
		});
		this.bind('touchmove',function (e) {
			this.isMove = true;
		});
		this.bind('touchend',function (e) {
			if (!this.isMove && this.isTouch) {
				callback(e);

			}
			this.isMove=false;
			this.isDrag=false;
			this.isTouch=false;
			this.isDouble=false;
			this.isMultipoint=false;
		});
	},
	// 双击
	doubleTouch:function () {
		
	},
	// 左右滑动
	slide:function () {
		
	},
	// 拖动
	drag:function () {
		
	},
	// 多点触控
	multipoint:function () {
		
	},
	// 多点滑动
	multipointSlide:function () {
		
	},
	run:function (obj) {
		this.obj = obj;
		return this;
	},
}


Touch.run(imgBox).touch(function (e) {
	console.log('单击事件');
})
console.log();


</script>

</body>
</html>